package com.sinosoft.dsp.component.bpm.facade.engine;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;

import org.jbpm.api.task.Task;

import com.sinosoft.dsp.component.bpm.util.JbpmConfig;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.log.Log;

/**
 * 执行流程服务门面
 * 
 * @author lichanghao
 * 
 */
@ManagedBean(name = "processTaskFacade")
@NoneScoped
public class ProcessTaskFacade {

	private static final Log log = Log
			.getLog(ProcessTaskFacade.class.getName());

	/**
	 * 完成任务
	 * 
	 * @param taskId
	 *            任务ID
	 */
	public boolean completeTask(String taskId) {
		try {
			JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
			jbpmConfig.getTaskService().completeTask(taskId);
			return true;
		} catch (Exception e) {

		}
		return false;
	}

	public boolean completeTask(String taskId, Map<String, Object> variables) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		jbpmConfig.getTaskService().completeTask(taskId, variables);
		return true;
	}

	public boolean completeTask(String taskId, String outcome) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		jbpmConfig.getTaskService().completeTask(taskId, outcome);
		return true;
	}

	public boolean completeTask(String taskId, String outcome,
			Map<String, Object> variables) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		jbpmConfig.getTaskService().completeTask(taskId, outcome, variables);
		return true;
	}

	/**
	 * 获得任务当前的流出转移名称
	 * 
	 * @param taskId
	 * @return
	 */
	public Set<String> getOutcomes(String taskId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		return jbpmConfig.getTaskService().getOutcomes(taskId);
	}

	/**
	 * 根据用户ID，获取已经正在处理的任务列表
	 * 
	 * @param userId
	 *            用户ID
	 * @return List 任务列表
	 */
	public List<Task> getAssignedTaskSetByUserId(String userId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		List<Task> task = jbpmConfig.getTaskService().findPersonalTasks(userId);
		return task;
	}

	/**
	 * 通过流程实例的ID获得任务
	 * 
	 * @param userId
	 * @param piId
	 * @return
	 */
	public Task getTaskByProcessInstanceId(String piId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		Task task = jbpmConfig.getTaskService().createTaskQuery()
				.processInstanceId(piId).uniqueResult();
		return task;
	}

	/**
	 * 根据用户ID，获取待办的任务列表
	 * 
	 * @param userId
	 * @return
	 */
	public List<Task> getUnassignedTaskSetByUserId(String userId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		List<Task> task = jbpmConfig.getTaskService().createTaskQuery()
				.unassigned().candidate(userId).list();
		return task;
	}

	/**
	 * 根据任务ID获取任务的相关变量
	 * 
	 * @param taskId
	 *            任务ID
	 * @return Map 变量的map
	 */
	public Map<String, Object> getTaskVariable(String taskId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		Set<String> set = jbpmConfig.getTaskService().getVariableNames(taskId);
		Map<String, Object> map = jbpmConfig.getTaskService().getVariables(
				taskId, set);
		return map;
	}

	/**
	 * 设置任务的相关变量
	 * 
	 * @param taskId
	 *            任务Id, variableMap任务变量Map
	 */
	public boolean setTaskVariable(String taskId,
			Map<String, Object> variableMap) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		jbpmConfig.getTaskService().setVariables(taskId, variableMap);
		return false;
	}

	/**
	 * 执行签入任务
	 * 
	 * @param taskId
	 *            任务ID
	 * @param userId
	 *            用户名
	 */
	public boolean assignTask(String taskId, String userId) {
		try {
			JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
			jbpmConfig.getTaskService().assignTask(taskId, userId);
			return true;
		} catch (Exception e) {
			log.error("执行签入任务出现错误。");
		}
		return false;
	}

}
